﻿using System;
using System.Windows.Forms;
using LDSModel.Dal;

namespace LDSProject.Forms
{
    public partial class UserManageForm : Form
    {
        public UserManageForm()
        {
            InitializeComponent();
        }

        private void UserManageForm_Load(object sender, EventArgs e)
        {
            btnQuery.PerformClick();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dialog = new UserModifyForm(this);
            dialog.ShowDialog(this);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dgRecord.DataSource = UserService.LoadUserList(tbUserName.Text.Trim());
        }

        private void tsResetPwd_Click(object sender, EventArgs e)
        {
            if (dgRecord.CurrentRow == null) return;
            var userId = Guid.Parse(dgRecord.CurrentRow.Cells[0].Value.ToString());
            if (!CheckSelf(userId)) return;
            var result = UserService.ResetPwd(userId);

            if (result.Success)
            {
                MessageBox.Show(this, @"密码重置为000000!");
            }
        }

        private void tsManage_Click(object sender, EventArgs e)
        {
            if (dgRecord.CurrentRow == null) return;
            var userId = Guid.Parse(dgRecord.CurrentRow.Cells[0].Value.ToString());
            if (!CheckSelf(userId)) return;
            var result = UserService.SetManager(userId, true);

            if (!result.Success) return;
            MessageBox.Show(this, @"管理员设置成功!");
            btnQuery.PerformClick();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (dgRecord.CurrentRow == null) return;
            var userId = Guid.Parse(dgRecord.CurrentRow.Cells[0].Value.ToString());
            if (!CheckSelf(userId)) return;
            var result = UserService.SetManager(userId, false);

            if (!result.Success) return;
            MessageBox.Show(this, @"普通用户设置成功!");
            btnQuery.PerformClick();
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            if (dgRecord.CurrentRow == null) return;
            var userId = Guid.Parse(dgRecord.CurrentRow.Cells[0].Value.ToString());
            if (!CheckSelf(userId)) return;
            var result = UserService.Delete(userId);

            if (!result.Success) return;
            MessageBox.Show(this, @"用户删除成功!");
            btnQuery.PerformClick();
        }

        public bool CheckSelf(Guid userId)
        {
            if (userId == UserService.CurrentUser.UserId)
            {
                MessageBox.Show(this, @"不能对自己进行操作!");
                return false;
            }
            return true;
        }

        private void dgRecord_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //判断如果点击的是鼠标右键  
            if (e.Button != MouseButtons.Right) return;
            //判断鼠标点击在数据行上  
            if (e.RowIndex < 0) return;
            dgRecord.ClearSelection();
            dgRecord.Rows[e.RowIndex].Selected = true;
            dgRecord.CurrentCell = dgRecord.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }
    }
}
